package main

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"log"
	"os"
)

func Loggerdemo() {
	file, _ := os.OpenFile("./web-6/log/logfile.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0744)
	log.SetOutput(file)
	log.Println("Error1")
}
func zapDemo() {
	//初始化一个logger
	var logger *zap.Logger
	logger, _ = zap.NewProduction()
	//Error级别日志
	logger.Error("Error fetching url", zap.String("url", "www.baidu.com"))
	//Info级别
	logger.Info("Info Success", zap.String("code", "200"))
	defer logger.Sync()
}
func sugaredLoggerdemo() {
	var logger *zap.Logger
	logger, _ = zap.NewProduction()
	//转为sugarlogger
	sugarlogger := logger.Sugar()
	defer sugarlogger.Sync()
	sugarlogger.Debugf("debugf is %s", "www.baidu.com")
	sugarlogger.Errorf("errorf is %s", "aaaa")
	sugarlogger.Infof("Infof is %s", "xxxx")
}
func zagToFileDemp() {
	//指定日志将写到哪里去
	file, _ := os.Create("./web-6/log/test.log")
	writeSyncer := zapcore.AddSync(file)
	//编码器(如何写入日志)
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder   //修改时间编码器
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder //在日志文件中使用大写字母记录日志级别
	//encoder:=zapcore.NewJSONEncoder(encoderConfig)
	encoder := zapcore.NewConsoleEncoder(encoderConfig)
	//哪种日志被写入
	core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
	//添加将调用函数信息记录到日志中的功能 AddCaller
	logger := zap.New(core, zap.AddCaller())

	sugarlogger := logger.Sugar()
	defer sugarlogger.Sync()
	sugarlogger.Debugf("debugf is %s", "www.baidu.com")
	sugarlogger.Errorf("errorf is %s", "aaaa")
	sugarlogger.Infof("Infof is %s", "xxxx")
}
func LumberJackDemo() {
	//指定日志将写到哪里去
	lumberJackLogger := &lumberjack.Logger{
		Filename:   "./web-6/log/test2.log",
		MaxSize:    10,
		MaxBackups: 5,
		MaxAge:     30,
		Compress:   false,
	}
	writeSyncer := zapcore.AddSync(lumberJackLogger)
	//编码器(如何写入日志)
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder   //修改时间编码器
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder //在日志文件中使用大写字母记录日志级别
	//encoder:=zapcore.NewJSONEncoder(encoderConfig)
	encoder := zapcore.NewConsoleEncoder(encoderConfig)
	//哪种日志被写入
	core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel)
	//添加将调用函数信息记录到日志中的功能 AddCaller
	logger := zap.New(core, zap.AddCaller())

	sugarlogger := logger.Sugar()
	defer sugarlogger.Sync()
	sugarlogger.Debugf("debugf is %s", "www.baidu.com")
	sugarlogger.Errorf("errorf is %s", "aaaa")
	sugarlogger.Infof("Infof is %s", "xxxx")
}
func main() {
	//Loggerdemo()
	//zapDemo()
	//sugaredLoggerdemo()
	//zagToFileDemp()
	LumberJackDemo()
}
